package com.ce.webframework.sec.util;

import java.util.ArrayList;
import java.util.List;

public class BackForward {
	List ls = new ArrayList();
	int step;

	public synchronized int record(String url) {
		try{
			ls.add(url);
			// new added
			if (ls.size() > 10) {
				ls = ls.subList(ls.size() - 10, ls.size());
			}
			return ls.size();
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("问题来了");
			e.printStackTrace();
			return 0;
		}

	}



	public String hback(int index) {
		int temp = 0;
		if ((ls.size() - index) <= 0) {
			return change((String) ls.get(0));
		} else {
			temp = index;
			int len = ls.size() - 1;
			step = len - temp;
			String url = (String) ls.get(step);
			return change(url);
		}
	}

	public String hforward(int index) {
		int temp = ls.size() - (index + 1);
		System.out.println("forward().backn=" + (ls.size() - index));
		String url = (String) ls.get(temp);
		return change(url);
	}

	// 判断是否为子菜单项，如果是则进行组合
	public String change(String urlid) {
		StringBuffer Rurlid = new StringBuffer();
		if (urlid.length() == 2){
			Rurlid.append(urlid);
		}
		else if (urlid.length() == 4) {
			Rurlid = Rurlid.append(urlid.substring(0, 2));
			Rurlid.append(",");
			Rurlid.append(urlid);
		}
		else if (urlid.length() == 6) {
			Rurlid = Rurlid.append(urlid.substring(0, 2));
			Rurlid.append(",");
			Rurlid.append(urlid.substring(0, 4));
			Rurlid.append(",");
			Rurlid.append(urlid);
		}
		return Rurlid.toString();
	}

}